package com.mifengs.mall.ui.msg;

import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.mall.entity.MsgBean;
import com.mifengs.mall.entity.ResponseBean;
import com.mifengs.mall.netWork.NetWork;

import java.util.HashMap;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by lynn on 2018/1/16.
 */

public class MsgCenterPresent implements MsgCenterContract.Presenter{
    private final int INT_MY_MSG = 0;
    private final int INT_SYSTEM_MSG = 1;
    private final int INT_PROMOTION_MSG =2;
    private MsgCenterContract.View mView;
    public MsgCenterPresent(MsgCenterContract.View mView) {
        this.mView = mView;
    }

    @Override
    public void getMsgData(final int type) {
        int pageNo = mView.setPage();
        HashMap<String,String> hashMap = new HashMap<>();
        hashMap.put("type",type+"");
        hashMap.put("pageNo",pageNo+"");
        hashMap.put("pageRow","100");
//        mView.showLoading();
        NetWork.getMsgCenterApi().getMsgCenter(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MsgBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
//                        mView.hideLoading();
                        mView.showNetError();

                    }

                    @Override
                    public void onNext(MsgBean msgBeanInfo) {
//                        mView.hideLoading();
                        MsgBean msgBean = msgBeanInfo;
                        int count = Integer.parseInt(msgBean.getRecordCount())-(Integer.parseInt(msgBean.getPageRow()) * mView.setPage());
                        if(count>0){
                            mView.getRecordCount(true);
                        }else{
                            mView.getRecordCount(false);
                        }
                        mView.getTotalCounter(Integer.parseInt(msgBean.getRecordCount()));
                        mView.setMsgContent(msgBean,type);

                    }
                });


    }

    @Override
    public void getMyMsgData() {
        int pageNo = mView.setPage();
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("app_token", PreferenceUtils.getToken());
        hashMap.put("pageNo",pageNo+"");
        hashMap.put("pageRow","100");
//        mView.showLoading();
        NetWork.getMyMsgApi().getMyMsg(hashMap)
                .subscribeOn(Schedulers.io())
                .compose(mView.<ResponseBean<MsgBean>>bindToLife())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBean<MsgBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
//                        mView.hideLoading();
                        mView.showNetError();

                    }

                    @Override
                    public void onNext(ResponseBean<MsgBean> msgBeanInfo) {
//                        mView.hideLoading();
                        MsgBean msgBean = msgBeanInfo.getParamer();
                        int count = Integer.parseInt(msgBean.getRecordCount())-(Integer.parseInt(msgBean.getPageRow()) * mView.setPage());
                        if(count>0){
                            mView.getRecordCount(true);
                        }else{
                            mView.getRecordCount(false);
                        }
                        mView.getTotalCounter(Integer.parseInt(msgBean.getRecordCount()));
                        mView.setMsgContent(msgBean,0);

                    }
                });
    }
}
